import QUANTAXIS as QA
from indicators import *

import tushare as ts
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler, StandardScaler
from sklearn.externals import joblib
import time
# pd.set_option('max_rows', 10000)
# pd.set_option('max_columns', 10000)


t = time.time()
end = ['2020-01-23', '2020-02-07', '2020-02-14', '2020-02-21', '2020-02-28', '2020-03-06', '2020-03-13', '2020-03-20', '2020-03-27']
start_date = '2001-01-01'
end_date = '2020-03-27'
code = '000001'


# data_w1 = QA.QA_fetch_stock_week_adv([code], '2018-07-01', '2019-9-30')
data = QA.QA_fetch_index_day_adv(code, start_date, end_date)
# data.kline_echarts(code=code)
# data.data.to_csv('data_index.csv')

print('已获取数据库数据：%04f' % (time.time() - t))

df_macd = data.add_func(QA.QA_indicator_MACD, short=10, long=30, mid=15)
df_rsi = data.add_func(QA.QA_indicator_RSI, N1=20)
df_kdj = data.add_func(QA.QA_indicator_KDJ)
df_bias = data.add_func(QA.QA_indicator_BIAS, N1=20, N2=20, N3=20)
df_label = data.add_func(QA.QA_indicator_LABEL, N=-5)
print('已计算完指标：%04f' % (time.time() - t))

df_data = data.data
df_data['MACD'] = df_macd.MACD
df_data['DIF'] = df_macd.DIF
df_data['DEA'] = df_macd.DEA
df_data['RSI'] = df_rsi.RSI1
df_data['KDJ_K'] = df_kdj.KDJ_K
df_data['KDJ_D'] = df_kdj.KDJ_D
df_data['KDJ_J'] = df_kdj.KDJ_J
df_data['BIAS'] = df_bias.BIAS1
df_data['LABEL'] = df_label.LABEL

df_data = df_data.iloc[41:]  # 删除有空指标的行
# df_data = df_data.reset_index()
# df_data.to_csv('data_index.csv')

mm_scale = MinMaxScaler()
column_x = ['close', 'high', 'low', 'open', 'volume', 'MACD', 'DIF', 'DEA', 'RSI', 'KDJ_K', 'KDJ_D', 'KDJ_J', 'BIAS', 'LABEL']
# column_x = ['close', 'high', 'low', 'open', 'volume', 'LABEL']
data_x = mm_scale.fit_transform(X=df_data[column_x])
data_y = df_data['LABEL'].to_numpy()
joblib.dump(mm_scale, 'index.scale')
df = pd.DataFrame(data_x)
# df.to_csv('data_index_scale.csv')
